<?php
/**
 * 语言设置，删除新增修改语言
 * @author Mortimer He <mortimerhe@163.com>
 */
namespace Manage\Controller;
use Common\Library\Page;
class LanguageController extends CommonController{
	private $msg=array();
	private $Model;
	function _initialize(){
		parent::_initialize();
		$this->Model=M('Language');
	}
	public function index(){
		if(!$this->checkPermission('Language')) $this->jump();
		$count=$this->Model->count();
		$Page=new Page($count,C('CONFIG_LIMIT_MANAGE'));
		$Page->setConfig('header',L('page_header'));
		$Page->setConfig('theme',L('page_theme'));
		$list=$this->Model->limit($Page->firstRow,$Page->listRows)->order('sort_order ASC')->select();
		$show=$Page->show();
		$url['p']=I('get.p','');
		//前端默认语言
		$this->assign('home_default_language',get_config_by_key('DEFAULT_LANG','Home'));
		$this->assign('url',$url);
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display('list');

	}
	public function oper($language_id=0){
		if(!$language_id && !$this->checkPermission('AddLanguage')) $this->jump();
		$map=array('language_id'=>$language_id);
		$language=$this->Model->where($map)->find();
		if($language_id && empty($language)) $this->jump(L('error_404'));
		if(IS_POST){
			$language=I('post.');
			if($this->validateForm()){
				 if($language_id){
				 	$this->Model->where($map)->save($language);
				 }else{
				 	$language['date_added']=time();
				 	$this->Model->add($language);
				 }
				 session('success',$language_id?L('success_edit'):L('success_add'));
				 $url['p']=I('get.p','');
				$this->redirect('index',$url);
			}else{
				if(!isset($this->msg['warning'])) $this->msg['warning']=L('error_warning');
			}
		}
		$this->assign('language',$language);
		$this->assign('msg',$this->msg);
		$this->display('oper');
	}
	private function validateForm(){
		$language_id = I('get.language_id',0,'trim');
		if($language_id && !$this->checkPermission('EditLanguage')){
			$this->msg['warning']=L('error_not_permission');
			return !$this->msg;
		}
		$data=I('post.');
		if(mb_strlen($data['name'])<2 || mb_strlen($data['name'])>50) $this->msg['name']=L('error_name');
		if(mb_strlen($data['image'])<5 || mb_strlen($data['image'])>50) $this->msg['image']=L('error_image');
		if(mb_strlen($data['code'])!=5) $this->msg['error_code']=L('error_code');
		if(mb_strlen($data['directory'])<2 || mb_strlen($data['directory'])>50) $this->msg['directory']=L('error_directory');
		if(mb_strlen($data['filename'])<2 || mb_strlen($data['filename'])>50) $this->msg['filename']=L('error_filename');
		return !$this->msg;
	}
	public function del(){
		if($this->checkPermission('DeleteLanguage')){
			$ids=I('post.selected');
			//如果要删除的语言列表里存在前端或后台正在使用的语言，则不予删除
			if(in_array(get_default_lng_id(),$ids) || in_array(get_config_by_key('DEFAULT_LANG','Home'),$ids)){
				session('danger',L('error_del_list_has_default'));
			}else{
				$map['language_id']=array('in',$ids);
				$count=$this->Model->where($map)->delete();
				session('success',L('success_delete',array('count'=>$count)));
			}
		}else{
			session('danger',L('error_not_permission'));
		}
		$this->redirect('index');
	}
}